<?php
	class Joob_Utils_Word
	{
	
		/**
        * @desc Check if word is empty.
        * @param string $a
        */
		public static function isEmpty(&$a){
			return Joob_Utils_Validator::isEmpty($a);
		}
		
		
		/**
        * @desc Placeholder.
        */
		public static function filter(&$a){
			
		}
		
		
		/**
        * @desc Clean string by remove unnecessary character.
        */
		public static function clean(&$a){
			$a=trim($a);
			return $a;
		}
		
		
		/**
        * @desc String compare.
        */
		public static function cmp($a,$b){
			self::clean($a);
			self::clean($b);
			if ($a==$b) return true;
			return false;
		}
		
		
        
		/**
        * @desc String compare case-insensitive.
        */
		public static function icmp($a,$b){
			$a=strtolower($a);
			$b=strtolower($b);
			return self::cmp($a,$b);
		}
		
		
		
        
        /**
        * @desc Getting the initial part of a string.
        * @param $work The string.
        * @param $l The length of the extracted string.
        */
		public static function initial(&$word,$l=1){
			return substr($word,0,$l);
		}
		
		
		
        /**
        * @desc Check if $find is a substring of $word.
        * @param $work The string.
        * @param $find The assumed substring.
        */
		public static function contain($word,$find){
			$a=strpos($word,$find);
			if ($a===false) return false;
			return true;
		}
		
		
        
		/**
        * @desc Split string $origin into an array. When splitting, only strings with
        * non-zero lengths are added to the returning array.
        * Example: Joob_Utils_Word::split("-"," i-your - company") return array("i","your","company");
        * @param $splitter The splitter that separates parts.
        * @param $origin The origin string.
        */
		public static function split($splitter,$origin){
			$s=explode($splitter,$origin);
			$r=array();
			for ($i=0; $i<count($s); $i++){
				$elem=$s[$i];
				$elem=self::clean($elem);
				if (strlen($elem)>=1)
				{
					$r[]=$elem;
				}
			}
			return $r;
		}
		
		
        /**
        * @desc Split string into part, but don't clean
        * whitespaces in each part.
        */
		public static function simpleSplit($splitter,$origin){
            $s=explode($splitter,$origin);
            return $s;
        }
        
        
        /**
        * @desc Alias of split.
        */
		public static function safeSplit($splitter, $origin){
			return self::split($splitter, $origin);
		}
		
		
		# Function: join($joinner,$origin)
		# ---------------------------------------------
		# Join an array and make it becomes a string.
		# Example:
		#	> Joob_Utils_Word::join("-",array("it","is","good")) return "it-is-good";
		
		public static function join($joinner, $origin){
			return implode($joinner,$origin);
		}
		
        
        /**
        * @uses Join part of an array. 
        */
        public static function joinPart($joinner, $origin, $from=0, $to=0){
            $result="";
            for ($i=$from; $i<=$to; $i++){
                $result.=$origin[$i];
                if ($i<$to){
                    $result.=$joinner;                        
                }
            }
            return $result;
        }
        
		
		# Function: shorten()
		# ---------------------------------------------
		# Get a substring of the current string, if the max length is smaller
		# than the length of the string. Additional $tail will be append to the
		# end of the string.
		# Example: 
		#	> Joob_Utils_Word::shorten('abcdefghijkl',7) return 'abcdefg...'
		
		public static function shorten($word,$max_length, $tail='...')
		{
            $more = (mb_strlen($word) > $max_length) ? true : false;
			if (!$more) return $word;
            
            $text = mb_substr($word, 0, $max_length, 'UTF-8');
			return $text.$tail;
		}
		
		
		# Function: safe_filter($str)
		# ---------------------------------------------
		# Return a string which contains only allowed (basic) characters. Those 
		# characters have index: 32, 45, 46, 65-90, 48-57.
		/*
		public static function safe_filter($word)
		{
			$result="";
			for ($i=0; $i<strlen($word); $i++){
				$ch=$word{$i};
				$d="";
				$ch=ord($ch)%256;
				if ($ch==45 || $ch==46 || ($ch>=65 && $ch<=90) || ($ch>=97 && $ch<=122) || $ch==32 || ($ch>=48 && $ch<=57))
				{
					$d=chr($ch);
				}
				$result=$result.$d;
			}
			return $result;
		}
		*/
		
		# Function: random($n)
		# ---------------------------------------------
		# Initialize a random string of length $n. This function is mostly for
		# security purpose.
		
		public static function random($n){
	
			if ($n<=0) $n=1;
			$s="";
			for ($i=0; $i<$n; $i++)
			{
				$ch=self::map_char(rand(0,61));
				$s.=$ch;
			}
			return $s;
		}
		
		
		# Function: in_array($word,$list)
		# -----------------------------------------------
		# Test if the string is in a list. The list can be either
		# an array or a string. 
		# Example:
		# 	> Joob_Utils_Word::in_array("abc", "abc def g) return true;
		# 	> Joob_Utils_Word::in_array("abc", array(" abc","def"," g") return true;
		#   > Joob_Utils_Word::in_array("abcd", array(" abc","def"," g") return false;
		
		public static function in_array($word,$list,$case=true){
			if (is_array($list))
			{
				for ($i=0; $i<count($list); $i++)
				{
					$s=$list[$i];
					if ($case){
						if (self::cmp($s,$word)){
							return true;
						}
					}else{
						if (self::icmp($s,$word)){
							return true;
						}
					}
				}
				return false;		
			}else{
				if (!is_string($list)) return false;
				if (self::cmp($word,$list)==true) return true;
				if (!$case && self::icmp($word,$list)==true) return true;
				return false;
			}
			return false;
		}
		
		
		# Function: inArray()
		# ----------------------------------------------
		# This is alias of in_array.
		
		public static function inArray($word, $list, $case=true){
			return self::in_array($word,$list,$case);
			
		}
		
		
		# Private function: map_char()
		# ----------------------------------------------
		# Helper of Joob_Utils_Word::random();
		
		private static function map_char($num) { 
			$int = $num; $int+=48;
			($int > 57) ? $int += 7 : null;
			($int > 90) ? $int += 6 : null;
			return chr($int);
		}
	}
?>